/*
 * @Author: Dorange.Wang
 * @Date: 2021-04-12 12:19:07
 * @LastEditors: your name
 * @LastEditTime: 2021-06-18 13:51:53
 * @Description: file content
 */
import showConfig from './showConfig'

export default {
  props: {
    modeler: {
      type: Object,
      required: true
    },
    modelData: {
      type: Object,
      default: () => {}
    },
    element: {
      type: Object,
      required: true
    },
    descriptor: {
      type: String,
      default: 'flowable'
    }
    // errorTab: {
    //   type: String,
    //   default: ''
    // }
  },
  data () {
    return {
      errorTab: null
    }
  },
  watch: {
    'formData.id': function (val) {
      this.updateProperty('id', val)
    },
    'formData.name': function (val) {
      this.updateProperty('name', val)
    },
    'formData.documentation': function (val) {
      if (val) {
        const documentation = this.modeler.get('moddle').create('bpmn:Documentation', { text: val })
        const newObjectList = []
        newObjectList.push(documentation)
        this.updateProperties({ documentation: newObjectList })
      } else {
        this.updateProperties({ documentation: undefined })
      }
    },
    'errorTab': function (val) {
      if (val) {
        this.activeTab = val
        this.errorTab = null
      }
    }
  },

  methods: {
    updateProperties (properties) {
      // debugger
      const modeling = this.modeler.get('modeling')
      modeling.updateProperties(this.element, properties)
    },
    updateProperty (propertyName, val, hasDescriptor) {
      if (val === '') {
        val = undefined
      }
      const properties = {}
      if (hasDescriptor) {
        propertyName = this.descriptorPrefix + propertyName
      }
      properties[propertyName] = val
      const modeling = this.modeler.get('modeling')
      modeling.updateProperties(this.element, properties)
    },
    getDocumentation () {
      const documentations = this.element.businessObject?.documentation
      const text = (documentations && documentations.length > 0) ? documentations[0].text : ''
      return text
    },
    convertDescriptorProperties (data) {
      // 移除 flowable: 或 camunda: 等前缀开头的变量，格式化数组
      for (const key in data) {
        if (key.indexOf(this.descriptorPrefix) === 0) {
          const newKey = key.replace(this.descriptorPrefix, '')
          data[newKey] = data[key]
          delete data[key]
        }
      }
      return data
    },
    async validate () {
      await this.$nextTick()
      return this.$refs?.refForm[0].validate()
    }

    // getShapeFields(){

    // }
  },
  computed: {
    // processFields () {
    //   // element.$parent.targetNamespace
    //   // console.log('this.element', this.element)
    //   // return this.element
    //   console.log('this.element 222', this.element)
    //   console.log('this.element 222', this.element.businessObject.$parent)
    // },

    descriptorPrefix () {
      return this.descriptor + ':'
    },
    elementType () {
      const bizObj = this.element.businessObject
      return bizObj.eventDefinitions
        ? bizObj.eventDefinitions[0].$type
        : bizObj.$type
    },
    customElementType () {
      // let submitValue = 'false'
      // const values = this.element.businessObject.extensionElements?.values
      // if (values) {
      //   const field = values.find(item => item.name === 'submit')
      //   submitValue = field?.string?.body
      // }

      // if (submitValue === 'true') {
      //   return 'bpmn:SubmitTask'
      // } else {
      //   return ''
      // }
      if (this.element.id === '__initiator__') {
        return 'bpmn:SubmitTask'
      } else if (this.element.id.startsWith('Script_')) {
        return 'bpmn:ServiceScriptTask'
      }
      return ''
    },
    showConfig () {
      // console.log('this.element 222', this.element)
      // console.log('this.element 222', this.element.businessObject.$parent)
      return showConfig[this.customElementType || this.elementType] || {}
    },
    onlyUserSettingEditor () {
      return this.modelData?.published === 1
    }
  }
}
